package com.wd.tech.adapter.News;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.wd.tech.R;
import com.wd.tech.bean.News_Show_Bean;
import com.wd.tech.view.activity.News_DetailsActivity;

import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * date:2020/7/2
 * author:王成虎(13671)
 * month:七月
 * function:
 */
public class NewsDetailsChannelAdapter extends RecyclerView.Adapter<NewsDetailsChannelAdapter.NewsDetailsChannelHolder> {



    private Context context;
    private List<News_Show_Bean.ResultBean> list;
    private SharedPreferences newsid;


    public NewsDetailsChannelAdapter(Context context, List<News_Show_Bean.ResultBean> list) {
        this.context = context;
        this.list = list;
    }

    @NonNull
    @Override
    public NewsDetailsChannelHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.news_show_layout, parent, false);
        return new NewsDetailsChannelHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull NewsDetailsChannelHolder holder, int position) {

        holder.showSim.setImageURI(list.get(position).getThumbnail());
        holder.textName.setText(list.get(position).getTitle());
        holder.textIntroduction.setText(list.get(position).getSummary());
        holder.textNickname.setText(list.get(position).getSource());
        holder.textCollect.setText(list.get(position).getCollection()+"");
        holder.textShare.setText(list.get(position).getShare()+"");
        Date date = new Date(list.get(position).getReleaseTime());
        int minutes = date.getMinutes();
        holder.textTime.setText(minutes+"分钟");
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, News_DetailsActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                newsid = context.getSharedPreferences("Newsid", Context.MODE_PRIVATE);
                newsid.edit().putInt("News_id",list.get(position).getId()).commit();
                context.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }


    public class NewsDetailsChannelHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.show_sim)
        SimpleDraweeView showSim;
        @BindView(R.id.text_name)
        TextView textName;
        @BindView(R.id.text_Introduction)
        TextView textIntroduction;
        @BindView(R.id.text_nickname)
        TextView textNickname;
        @BindView(R.id.text_time)
        TextView textTime;
        @BindView(R.id.text_Collect)
        TextView textCollect;
        @BindView(R.id.text_share)
        TextView textShare;
        public NewsDetailsChannelHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
